package com.practice.system.service.impl;

import com.practice.system.mapper.LinkPlanningDetailchedulingMapper;
import com.practice.system.mapper.PlanningDetailMapper;
import com.practice.system.mapper.PlanningSchedulingMapper;
import com.practice.system.pojo.PlanningScheduling;
import com.practice.system.service.PlanningSchedulingService;
import com.practice.utils.PageUtils;
import com.practice.utils.Query;
import com.practice.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class PlanningSchedulingServiceImpl implements PlanningSchedulingService {
    @Autowired
    PlanningSchedulingMapper planningSchedulingMapper;
    @Autowired
    LinkPlanningDetailchedulingMapper linkPlanningDetailchedulingMapper;
    @Autowired
    PlanningDetailMapper planningDetailMapper;

    @Override
    public R list(Map<String, Object> params) {
        new Query().setStartPage(params);
        //这里查询出上半部分以及一对一的关系
        List<Map<String, Object>> resultMap = planningSchedulingMapper.selectByParams(params);
        // ==>这里没有实现，因为Map转换不过来com.practice.system.pojo.PlanningScheduling cannot be cast to java.util.Map，使用单查的方法
//        //这里对下半部分的对多进行查询，查询出物料表的list:
//        System.out.println(resultMap);
//        for (Map<String, Object> stringObjectMap : resultMap) {
//            //这里存放物料表的list
//            List<Map<String, Object>> MaterielListResult = new ArrayList<>();
//            List<String> MaterielList = linkPlanningDetailchedulingMapper.listByPlanningChedulingId(stringObjectMap.get("linkPlanningDetailchedulingId").toString());
//            //拿到物料表的list，进行查询物料表，并绑定到MaterielListResult
//            for (String I : MaterielList) {
//                MaterielListResult.add((Map<String, Object>) planningDetailMapper.findById(I));
//            }
//            stringObjectMap.put("planningDetail", MaterielListResult);
//        }
        Integer count = planningSchedulingMapper.count(params);
//        //封装返回类
        PageUtils pageUtils = new PageUtils(resultMap, count);
        return R.ok().put("page", pageUtils);
    }

    @Override
    public R update(PlanningScheduling updateValue) {
        return planningSchedulingMapper.update(updateValue).equals(true) ? R.ok() : R.error();
    }

    @Override
    public R add(PlanningScheduling planningScheduling) {
        if (planningSchedulingMapper.add(planningScheduling))
            return R.ok();
        else
            return R.error();
    }

    @Override
    public R delete(String id) {
        if (planningSchedulingMapper.delete(id) & linkPlanningDetailchedulingMapper.deleteByScheduling(id))
            return R.ok();
        else
            return R.error();
    }
}
